package edu.mse.camel.client.whiteboard.gef.ui.commands;


import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

import edu.mse.camel.client.whiteboard.gef.ui.freehandwriting.FreeHandwritingCreateRequest;
import edu.mse.camel.client.whiteboard.gef.whiteboard.FreeWritingShape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardFactory;
import edu.mse.camel.client.whiteboard.gef.whiteboard.XY;


public class FreeWritingShapeCreateCommand extends AbstractWhiteboardCommand {

	
	private FreeWritingShape freeWritingShape;
	
	
	private  WhiteboardDiagram parent;
	private final FreeHandwritingCreateRequest request;
	private boolean shapeAdded;
	private Rectangle bound;
	
	public FreeWritingShapeCreateCommand(
			WhiteboardDiagram diagram, FreeHandwritingCreateRequest req, Rectangle bound) {
	    if (req == null || !(req.getNewObject() instanceof FreeWritingShape)) {
	        throw new IllegalArgumentException();
	    }
	
	    this.parent = diagram;
	    this.request = req;
	    freeWritingShape = (FreeWritingShape) request.getNewObject();
	    setLabel("Create "+((FreeWritingShape) req.getNewObject()).toString()+" shape");
	    this.bound = bound;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
	    return shapeAdded;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
    
	    //System.out.println(request);
   
	    if(request.getSize() ==  null)
	        request.setSize(new Dimension(20,20));
	        
	    int diffX = bound.x - request.getLocation().x;
		int diffY = bound.y - request.getLocation().y;
		
	    int min_x = 10000;
	    int min_y = 10000;
	    List <XY> points = request.getPoints();
	    for(int i=0; i < points.size(); i++) {
	        XY p = points.get(i);
	        if(p.getX() < min_x)
	            min_x = p.getX();
	        if(p.getY() < min_y)
	            min_y = p.getY();
	    }
	    min_x = min_x + diffX;
	    min_y = min_y + diffY;
	    
	    int max_x = 0;
	    int max_y = 0;
	    for(int i=0; i < points.size(); i++) {
	        XY p = points.get(i);
	        p.eSetDeliver(false);
	        p.setX(p.getX() + diffX - min_x);
	        p.setY(p.getY() + diffY - min_y);
	        p.eSetDeliver(true);
	        if(p.getX() > max_x)
	            max_x = p.getX();
	        if(p.getY() > max_y)
	            max_y = p.getY();
	    }
	    //max_x = max_x + diffX;
	    //max_y = max_y + diffY;
	    
	    freeWritingShape.eSetDeliver(false);
		for (XY xy : request.getPoints()) {
			XY newXY = WhiteboardFactory.eINSTANCE.createXY();
			newXY.eSetDeliver(false);
			newXY.setX(xy.getX());
			newXY.setY(xy.getY());
			newXY.eSetDeliver(true);
			freeWritingShape.getPoints().add(newXY);
		}
	    freeWritingShape.eSetDeliver(true);
	    freeWritingShape.setLocation(new Point(min_x, min_y));
	    freeWritingShape.setSize(new Dimension(max_x + 1, max_y + 1));
	    freeWritingShape.setColor(request.getColor().getRGB());
	    
	    shapeAdded = parent.getFreeWritingShapes().add(freeWritingShape);
	    
	}
		
	public boolean canExecute() {
	    //if(parent == null || newShape == null || request == null)
	    if(freeWritingShape  == null || request == null)
	        return false;
	       
	    return true;
	}
	    
}
